package com.qf.xiangmu.contrller;

import com.github.pagehelper.PageInfo;
import com.qf.xiangmu.dao.GdDao;
import com.qf.xiangmu.pojo.Gd;
import com.qf.xiangmu.pojo.ResultBean;
import com.qf.xiangmu.service.GdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zzj
 * @version V1.0
 * @date 2021/10/30
 **/
@RestController
@RequestMapping("/gd")
@CrossOrigin
public class GdContrller {

    @Autowired
    private GdService gdService;

    @Autowired
    private GdDao gdDao;

    /**
     * 查询全部的工地列表
     * @return
     */
    @RequestMapping("/selectAll")
    public List<Gd> selectAllGd(){
        return gdService.selectAll();
    }

    @RequestMapping("/addGd")
    public ResultBean insertSelective(@RequestBody Gd gd){
        ResultBean resultBean = new ResultBean();
        if (gd.getGdName().equals("")){
            resultBean.setMassage("失败");
            resultBean.setCode(30);
            return resultBean;
        }
        if(gd.getGdInfo().equals("")){
           gd.setGdInfo("该工地未描述");
        }
        if(gd.getGdAddr().equals("")){
            gd.setGdAddr("该设备未描述");
        }
        int i = gdDao.selectByName(gd.getGdName());//判断名字，有没有存在该工地 i=1就是有 i=0就是无

        if(i==1){
            resultBean.setMassage("已存在该工地");
            resultBean.setCode(10);
            return resultBean;
        }else{
            int i1 = gdService.insertSelective(gd);//如果返回1，就是成功，如果返回0，就是失败
            if(i1==1){
                resultBean.setMassage("成功注册");
                resultBean.setCode(100);
                return resultBean;
            }else {
                resultBean.setMassage("已存在工地名");
                resultBean.setCode(20);
                return resultBean;
            }
        }




       /* //判断有没有这个工地 1为有  0 为没有
        if (i==1){
            resultBean.setMassage("失败");
            resultBean.setCode(10);
            return resultBean;
        }else {
            //判断有没有这个工地名字
            Integer code = gdService.insertSelective(gd);
            if(code==0){
                resultBean.setMassage("成功");
                resultBean.setCode(1000);
                return resultBean;
            }else {
                resultBean.setCode(500);
            }
        }*/
    }

    /**
     * 查询全部的工地列表(分页)
     * @return
     */
    @RequestMapping("/getGdByPage/{page}/{limit}")
    public ResultBean getUsersByPage(@PathVariable Integer page, @PathVariable Integer limit){
        ResultBean resultBean = new ResultBean();
        PageInfo pageInfo = gdService.selectAllGd(page, limit);
        resultBean.setCode(1000);
        resultBean.setMassage("查询成功");
        resultBean.setData(pageInfo);
        return resultBean;
    }

    @RequestMapping("/getGdById")
    public Gd getGdById(Integer id){
        return gdService.selectByPrimaryKey(id);
    }
    /**
     * 通过id删除一个设备
     * @param id
     * @return
     */
    @RequestMapping("/delet/{id}")
    public ResultBean deletSbById(@PathVariable Integer id){
        ResultBean resultBean = new ResultBean();
        Integer code = gdService.deletGdById(id);
        if(code==1){
            resultBean.setCode(1000);
            resultBean.setMassage("成功");
            System.out.println("成功-------------"+resultBean);
            return resultBean;
        }else if(code==-1){
            resultBean.setCode(1);
            resultBean.setMassage("失败");
            System.out.println("失败-------------"+resultBean);
            return resultBean;
        }else {
            resultBean.setCode(code);
            resultBean.setMassage("失败");
            System.out.println("失败-------------"+resultBean);
            return resultBean;
        }
    }

    @RequestMapping("/update")
    public ResultBean updateGd(@RequestBody Gd gd){
        ResultBean resultBean = new ResultBean();
        Integer code = gdService.updateGd(gd);
        if(code==1){
            resultBean.setCode(1000);
            return resultBean;
        }
        return resultBean;
    }


}
